package com.library.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.library.entity.BookCommentInfo;
import com.library.entity.RUserLike;
import com.library.entity.RecommendManageEntity;
import com.library.entity.ResourceEntity;
import com.library.entity.dto.BookCommentInfoDto;
import com.library.mapper.BookCommentInfoMapper;
import com.library.service.BookCommentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.service.RUserLikeService;
import com.library.service.ResourceService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * <p>
 * book评论
 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-10-20
 */
@Service
public class BookCommentInfoServiceImpl extends ServiceImpl<BookCommentInfoMapper, BookCommentInfo> implements BookCommentInfoService {

    @Resource
    private ResourceService resourceService;

    @Resource
    private RUserLikeService rUserLikeService;

    @Override
    public boolean edit(BookCommentInfoDto dto) {
        LambdaQueryWrapper<BookCommentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BookCommentInfo::getBookCode,dto.getBookCode());
        BookCommentInfo bookCommentInfo=this.getOne(wrapper);
        if(bookCommentInfo ==null){
            ResourceEntity resourceEntity=resourceService.getById(Long.parseLong(dto.getBookCode()));
//            if(resourceEntity ==null){
//                throw new RuntimeException("本地库资源不存在,请核查!");
//            }
            BookCommentInfo add=new BookCommentInfo();
            add.setBookCode(dto.getBookCode());
            //add.setBookName(resourceEntity.getResourceName()+"");
            add.setTotalReadCount(0);
            add.setCompleteReadCount(0);
            add.setCommentCount(1);
            if(dto.getCommentType()==1){
                add.setRecommendCount(1);
            }
            if(dto.getCommentType()==2){
                add.setCommonCount(1);
            }
            if(dto.getCommentType()==3){
                add.setBadCount(1);
            }

            add.setCollectCount(0);
            add.setSfRead(1);
            add.setCreateTime(new Date());
            double result = (double)add.getRecommendCount() / (double)add.getCommentCount(); // 计算相除结果
            DecimalFormat df = new DecimalFormat("0.00%"); // 格式化为百分数
            String percentage = df.format(result);
            add.setRecommendValue(percentage);
            this.save(add);
        }else{
            if(dto.getCommentType()==1){
                bookCommentInfo.setRecommendCount(
                        bookCommentInfo.getRecommendCount() !=null
                        ?bookCommentInfo.getRecommendCount()+1:1);
            }
            if(dto.getCommentType()==2){
                bookCommentInfo.setCommonCount(
                        bookCommentInfo.getCommonCount() !=null
                                ?bookCommentInfo.getCommonCount()+1:1);
            }
            if(dto.getCommentType()==3){
                bookCommentInfo.setBadCount(
                        bookCommentInfo.getBadCount() !=null
                                ?bookCommentInfo.getBadCount()+1:1);
            }
            bookCommentInfo.setRecommendCount(
                    bookCommentInfo.getRecommendCount() !=null
                            ?bookCommentInfo.getRecommendCount():0);
            bookCommentInfo.setCommonCount(
                    bookCommentInfo.getCommonCount() !=null
                            ?bookCommentInfo.getCommonCount():0);
            bookCommentInfo.setBadCount(
                    bookCommentInfo.getBadCount() !=null
                            ?bookCommentInfo.getBadCount():0);
            bookCommentInfo.setCommentCount(bookCommentInfo.getRecommendCount()+bookCommentInfo.getCommonCount()+bookCommentInfo.getBadCount());

            LambdaQueryWrapper<RUserLike> wrapper0=new LambdaQueryWrapper<>();
            wrapper0.eq(RUserLike::getType,2);
            wrapper0.eq(RUserLike::getUserId,dto.getUserId());
            wrapper0.eq(RUserLike::getResourceId,dto.getBookCode());
            RUserLike rUserLike=rUserLikeService.getOne(wrapper0);
            if(rUserLike !=null){
                if(rUserLike.getCommentType()==1){
                    if(bookCommentInfo.getRecommendCount() !=null && bookCommentInfo.getRecommendCount() >0){
                        bookCommentInfo.setRecommendCount(bookCommentInfo.getRecommendCount()-1);
                    }
                }
                if(rUserLike.getCommentType()==2){
                    if(bookCommentInfo.getCommonCount() !=null && bookCommentInfo.getCommonCount() >0){
                        bookCommentInfo.setCommonCount(bookCommentInfo.getCommonCount()-1);
                    }
                }
                if(rUserLike.getCommentType()==3){
                    if(bookCommentInfo.getBadCount() !=null && bookCommentInfo.getBadCount() >0){
                        bookCommentInfo.setBadCount(bookCommentInfo.getBadCount()-1);
                    }
                }
                if(bookCommentInfo.getCommentCount() !=null && bookCommentInfo.getCommentCount()>0){
                    bookCommentInfo.setCommentCount(bookCommentInfo.getCommentCount()-1);
                }
                rUserLikeService.removeById(rUserLike);
            }
            double result = (double)bookCommentInfo.getRecommendCount() / (double)bookCommentInfo.getCommentCount(); // 计算相除结果
            DecimalFormat df = new DecimalFormat("0.00%"); // 格式化为百分数
            String percentage = df.format(result);
            bookCommentInfo.setRecommendValue(percentage);
            this.updateById(bookCommentInfo);
        }

        RUserLike rUserLike1=new RUserLike();
        rUserLike1.setUserId(dto.getUserId());
        rUserLike1.setResourceId(dto.getBookCode());
        rUserLike1.setType(2);
        rUserLike1.setCommentType(dto.getCommentType());
        rUserLike1.setCreateTime(new Date());
        rUserLikeService.save(rUserLike1);

        return true;
    }
}
